package com.art.wisdom;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;



@Configuration
@EnableAutoConfiguration //(@SpringBootApplication 和  @EnableAutoConfiguration 意思是一样)
@ComponentScan
@MapperScan("com.art.wisdom.mapper")
public class App implements EmbeddedServletContainerCustomizer{
		
		//数据库配置文件加载
		@Bean
		@ConfigurationProperties(prefix = "spring.datasource")
		public DataSource dataSource() {
			return new org.apache.tomcat.jdbc.pool.DataSource();
		}
		
		//mybatis文件加载
		@Bean
		public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
	
			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
			sqlSessionFactoryBean.setDataSource(dataSource());
	
			PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	
			sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
	
			return sqlSessionFactoryBean.getObject();
		}
		
		@Bean
		public PlatformTransactionManager transactionManager() {
			return new DataSourceTransactionManager(dataSource());
		}
	
		public static void main(String[] args) {
	
			SpringApplication.run(App.class, args);
		}
		
		/**
		 * 设置端口号
		 */
		@Override  
	    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {  
	  
	        configurableEmbeddedServletContainer.setPort(8081);  
	    }  
}
